<?php
namespace Admin\Controller;
use Common\Common\Tree;
use Admin\Common\CommonController; //使用并继承CommonController类，用作权限的授权
header("Content-type: text/html; charset=utf-8");
class CategoryController extends CommonController{

   //加载模板
	public function articleClassAction(){
		$model = M('category');
		$categorys = $model->select();
		// var_dump($categorys);
		// exit();
		$obj = new Tree();//在公共文件中建一个类，用于数据的重组
		$categorys = $obj->reload($categorys);
		// var_dump($categorys);
		// exit();
		$this->assign('categorys',$categorys);//分配数据到模板中
		$this->display();//显示模板
	}

	//添加分类
	public function addArticleClassAction(){
		if(IS_POST){
			// var_dump(I('post.'));
			// exit();
			$data['fid'] = I('post.fid',0);//不传默认父id为0（z）
			$data['cname'] = I('post.cname');
			$data_m = M('category');
			$res = $data_m->add($data);
			//用模板中的add方法而不用execute好处是会返回插入的ID
			// var_dump($data);
			// exit();
			if($res){
				$this->success('增加成功',U('Admin/Category/articleClass'),2);
			}else{
				$this->error('增加失败',U('Admin/Categoryn/articleClass'),2);
			}		
		}else{
   		   $this->error('非法请求',U('Admin/Category/index'));
    	}
	}


	//修改分类
	public function articleClassEditAction(){
		// var_dump(I('get.id'));
		// exit();		
		if(IS_POST){
				$category_id = I('get.id');
				$data_class_value = I('post.class-val');
				$sql = "update category set cname = '$data_class_value' where id = '$category_id' ";
				// var_dump($category_id);
				// exit();
				$data_m = M();//实例化数据库模型
				$res = $data_m->execute($sql);	
				if($res){
					$this->success("修改成功",U('Admin/Category/articleClass'),2);
				}else{
					$this->error("修改失败",U('Admin/Category/articleClass'),2);
				}		
			}else{		
				$category_id = I('get.id');	
			    $sql = "select * from category where id=$category_id";
				$model = M();
				$categorys = $model->query($sql);
				$categorys = current($categorys);
				// var_dump($categorys);
				// exit();
				$this->assign('categorys',$categorys);//分配数据到模板中
				$this->display();//显示模板
			}
		
	}

	//删除分类
	public function articleClassDelAction(){
		if(IS_GET){
				$category_id = I('get.id');//找到要删除的ID
				$sql = "select * from category where fid = '$category_id'"; //可以找fid 作为条件，如果有找到符合的，则说明不能删，有子集
				$data_m = M();//实例化数据库模型
				$res = $data_m->query($sql);//先找出所有符合要求的
				if($res){
					$this->error("下级有子分类，删除失败",U('Admin/Category/articleClass'),2);
				}else{
					$sql_d = "delete from category where id= '$category_id'";
					$res_category = $data_m->execute($sql_d);
					// var_dump($res_category);
					if($res_category){
						$this->success("修改成功",U('Admin/Category/articleClass'),2);
					}else{
						$this->error("操作失败",U('Admin/Category/articleClass'),2);
					}
					
				}						
		}
	}


	//文章管理
	public function articleListAction(){
		$sql = "select t1.id,t1.time,t1.title,t2.username,t3.cname from question t1 left join user t2 on t1.uid=t2.id left join category t3 on t1.cid=t3.id";
		$data_m = M();
		$question_res = $data_m->query($sql);
		// var_dump($question_res);
		// exit();
		$this->assign('question_res',$question_res);//分配数据到模板中
		$this->display();//显示模板
	}

	public function articleDetailShowAction(){
		// var_dump(I('get.'));
		$this->display();//显示模板
	}

	//添加文章
	public function addArticleAction(){

		// var_dump(I('get.'));
		$this->display();//显示模板
	}
	
	//删除文章
	public function delArticleAction(){	
		// var_dump(I('post.'));
		$aid = I('post.aid');
		$sql = "delete from question where id='$aid'";
		$data_m=M();
		$res=$data_m->execute($sql);
		$arr = array('success'=>$res);
		echo json_encode($arr);
	}


	//话题管理
	public function themeListAction(){
		$sql = "select * from theme";
		$data_m = M();
		$question_res = $data_m->query($sql);
		// var_dump($question_res);
		// exit();
		$this->assign('question_res',$question_res);//分配数据到模板中
		$this->display();//显示模板
	}

	//添加话题
	public function addThemeAction(){
		if(IS_POST){
			$themeName=I('post.themeName');
			$sql = "insert into theme(tname) values('$themeName')";
			$data_m=M();
			$res = $data_m->execute($sql);
			if($res){
				$this->success("添加话题成功",U('Admin/Category/userSuccess'),2);
			}else{
				$this->error("添加话题失败",U('Admin/Category/themeList'),2);
			}
		}else{
			$this->display();//显示模板
		}

	}
    
    //给一个新的方法让弹出的窗口关闭
	public function userSuccessAction()
	{    
		$this->display();
	}

	//编辑话题
	public function editThemeAction(){
		//当有POST传输数据时
		if(IS_POST){
			$themeId=I('post.themeid');
			$username=I('post.user-name');
			$sql="update theme set tname='$username' where id='$themeId'";
			$data_m=M();
			$result=$data_m->execute($sql);
			// var_dump($theme_res);
			// exit();
			$arr = array('success' =>$result);
			echo json_encode($arr);
			
		}elseif(IS_GET){	
		//当点击编辑时							
			$themeid=I('get.id');
			$sql="select * from theme where id='$themeid'";
			$data_m=M();
			$theme_res=$data_m->query($sql);
			$theme_res=current($theme_res);
			// var_dump($theme_res);
			// exit();
			$this->assign("theme_res",$theme_res);
			$this->display();
		}
		
	}

	//删除话题
	public function delThemeAction(){
		// var_dump(I('post.themeid'));
		// exit();
		$themeId=I('post.themeid');
		$sql="delete from theme where id='$themeId'";
		$data_m=M();
		$theme_res=$data_m->execute($sql);
		$arr=array('success'=>$theme_res);
		echo json_encode($arr);
	}

}

?>